package itsm.isperp.workflow.listener;

import itsm.isperp.workflow.utils.BpmScriptUtils;

import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.delegate.TaskListener;
import org.activiti.engine.impl.persistence.entity.TaskEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 基础的任务监听器
 * 
 * @author lizx
 * @date 2013-4-25
 */
public abstract class BaseTaskListener implements TaskListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	protected Logger logger = LoggerFactory.getLogger(getClass());

	@Override
	public void notify(DelegateTask delegateTask) {
		logger.debug("enter the baseTaskListener notify method...");
		TaskEntity taskEnt = (TaskEntity) delegateTask;
		String nodeId = taskEnt.getExecution().getActivityId();
		String defId = taskEnt.getProcessDefinitionId();
		DelegateExecution delegateExecution = delegateTask.getExecution();

		boolean b = execute(delegateTask, delegateExecution, defId, nodeId);

		if (b) {
			try {

				delegateTask
						.setVariable("processTaskId", delegateTask.getId());
				BpmScriptUtils.exeEventScript(delegateTask, delegateExecution,
						getScriptType(), defId, nodeId);
			} catch (Exception e) {
				logger.error(e.getMessage(), e);// 记录异常的发生;
			}
		}
	}

	/**
	 * 执行流程节点上所定义的监听器
	 * 
	 * @param delegatetask
	 *            任务实体
	 * @param delegateExecution
	 *            任务执行器
	 * @param defId
	 *            流程定义的id
	 * @param nodeId
	 *            流程节点id
	 * @return true表示继续执行父类的脚本定义中的方法，false表示被执行
	 */
	protected abstract boolean execute(DelegateTask delegatetask,
			DelegateExecution delegateExecution, String defId, String nodeId);

	protected abstract String getScriptType();

}